﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Trade.DBUtility;
using Trade.DBUtility.DataTableName;
using TradeWeb.Models;
using System.Web.Script.Serialization;
using System.IO;
using System.Dynamic;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Configuration;
namespace TradeWeb.Controllers
{
    public class PDNewController : Controller
    {
        //
        // GET: /PD/

        public ActionResult Index()
        {
            return View();
        }
        public string GetPDList()
        {
            string jsonResult = string.Empty;
            //构造反馈对象
            dynamic damResult = new ExpandoObject();
            damResult.success = false;
            damResult.message = "获取失败";
            DataSet ds = new DataSet();
            try
            {
                ds = CustomizableDesignBLL.GetData("select top 10 * from T_PD order by SerialNum desc");
            }
            catch (Exception ex)
            {
                damResult.message = "获取模块数据表结构失败:" + ex.Message;
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            if (ds.Tables[0].DefaultView.Count > 0)
            {
                List<dynamic> varList = new List<dynamic>();
                foreach (DataRowView drv in ds.Tables[0].DefaultView)
                {
                    var item = Trade.DBUtility.ExtendObject.AutoMap(drv.Row);
                    varList.Add(item);
                }
                damResult.success = true;
                damResult.data = varList;
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            jsonResult = "{\"success\":false   ,\"message\":\"请先于PC端创建盘点单\"}";
            return jsonResult;
        }
        public string GetXLCodeInfo(string BarCode, string SerialNum, string UserCode)
        {
            string jsonResult = string.Empty;
            //构造反馈对象
            dynamic damResult = new ExpandoObject();
            damResult.success = false;
            damResult.message = "";
            if (UserCode == "")
            {
                damResult.message = "请先登录";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            //
            string strConn = ConfigurationManager.ConnectionStrings["ConStr2"].ConnectionString;
            ADODB.Connection oCon = new ADODB.Connection();
            oCon.CursorLocation = ADODB.CursorLocationEnum.adUseClient;
            oCon.ConnectionString = strConn;
            oCon.Open(strConn, "", "", -1);
            oCon.BeginTrans();
            DataSet ds = new DataSet();
            string SQL = "select InvCode cInvCode,cInvName,OrderSerialID XLCode,cWhCode,cBatchNo cBatch,iSNState from t_dispatch_serialno a " +
            "left join " + Common.GetdatabaseName("") + ".dbo.ST_SNState b on OrderSerialID=cInvSN where OrderSerialID='" + BarCode + "'";
            try
            {
                ds = CustomizableDesignBLL.GetData(SQL);

                if (ds.Tables[0].DefaultView.Count == 0)
                {
                    jsonResult = "{\"success\":false   ,\"message\":\"此序列号不存在\"}";
                    return jsonResult;
                }
                string KCQty = "0";
                string iSNState = ds.Tables[0].DefaultView[0]["iSNState"].ToString();
                if (iSNState == "2")
                {
                    KCQty = "1";
                }

                string cInvCode = ds.Tables[0].DefaultView[0]["cInvCode"].ToString();
                string cInvName = ds.Tables[0].DefaultView[0]["cInvName"].ToString();
                string cBatch = ds.Tables[0].DefaultView[0]["cBatch"].ToString();
                string cWhCode = ds.Tables[0].DefaultView[0]["cWhCode"].ToString();
                string cWhName;
                if (cWhCode == "") cWhCode = "0401";
                if (cWhCode == "0401")
                    cWhName = "成品仓";
                else if (cWhCode == "0402")
                    cWhName = "采购仓";
                else
                    cWhName = "售后仓";
                string pdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string type;
                decimal qty = 1;
                decimal kcqty = decimal.Parse(KCQty);
                if (qty == kcqty) type = "正常";
                else if (qty < kcqty) type = "盘亏";
                else type = "盘盈";
                string sql = "insert into T_PDProduct(SerialNum,GUID,ISDel,Qty,KCQty,XLCode,cInvCode,cInvName,iSNState," +
                "cBatch,cWhCode,cWhName,pdDate,type)values('" + SerialNum + "','" + UserCode + "',0,1," + KCQty + ",'" + BarCode + "','" +
                cInvCode + "','" + cInvName + "'," + iSNState + ",'" + cBatch + "','" + cWhCode + "','" + cWhName + "','" + pdDate + "','" + type + "')";
                //添加记录
                object objADO = null;
                int iOptions = 0;
                oCon.Execute(sql, out objADO, iOptions);
                //检测是否已扫描
                ADODB.Recordset rs = new ADODB.Recordset();
                string strRsSQL = "Select * from T_PDProduct where SerialNum='" + SerialNum + "'and XLCode='" + BarCode + "'";
                rs.Open(strRsSQL, oCon, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, (int)ADODB.CommandTypeEnum.adCmdText);
                if (rs.RecordCount > 1)
                {
                    oCon.RollbackTrans();
                    damResult.message = "此序列号已扫描";
                    jsonResult = JsonConvert.SerializeObject(damResult);
                    return jsonResult;
                }
                oCon.CommitTrans();
                List<dynamic> varList = new List<dynamic>();
                foreach (DataRowView drv in ds.Tables[0].DefaultView)
                {
                    var item = Trade.DBUtility.ExtendObject.AutoMap(drv.Row);
                    varList.Add(item);
                }
                damResult.success = true;
                damResult.data = varList;
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            catch (Exception ex)
            {
                oCon.RollbackTrans();
                damResult.message = "错误信息：" + ex.Message;
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
        }
    }
}
